package com.example.testdemoutils.toppay;


import com.google.gson.Gson;

import java.util.Map;
import java.util.TreeMap;

public class TopPayDemo {
    // 测试账号
    private static final String MCH_ID = "S820211021094748000001";  // 此乃测试商户号,请使用自己申请的真实商户号和对应的密钥
    private static final String PLAT_PUBLIC_KEY = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC2JoMfFqLsSJjAiCahEnlP3aRj8yCT+WHzR+VvPBTw9S1i7iYWb+MY09CG/HYuHF4+IxshXDJygmndxKf/esuwPybS8mAd//yubHpmZsmBqg1FffT8VH1APa6ZRWASUp4U01ZrbCCp35QA8FuWrJGMJxGx4xk7KUtV2yujxC8noQIDAQAB";  // 平台公钥
    private static final String MCH_PRIVATE_KEY = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJU8gKFKD0luIYx7X8+JRdCIE0UDBctS6LjXxWLEv/EO7jDBTid6zYP1KmNgpd2DAWWtBFBSQ+gcNwVZZSBHJiSDqVvvJVs2FEbeBvfdv4X93+IYRAXksBasSW5Tpdshbo82pVL4V7wuKCuFLk9UxBHbpQjWAbfyF66RmwIbZD71AgMBAAECgYBjPe7UU2nDDSfmQg0++CyjNjqKRC5QPfxhH6w1uF1kMueXKJWOj42n2RutJpJmsj31nY8m0u4xpsG4HvCu/GGSFhhKZCHLvzp41oY2ubYj9nuFNU//81LycQjulWo2y0UUBY0k2piEt+SwPaiUNbT6nMxNMjlnjRe2okp/3rw+KQJBANG3YlZWoVbCEqzy64bJJLxiPsCA5ErGB0NzRGitq44xkhqGtR8ZZQyVz40pruNa58d73O2xyJSy5+fmZGn4E+sCQQC2LBnguj0CSCKub0mPDcunTTz9V79VXBBZdlB1/YGmRUx2s4sQrJNZS7rL4EqBQ3maIRnG+s+AXCSTfsYrV6CfAkEAxugnVfpelhoGepEAgNuggyivmgfl/2Gpm/jk5l/qOjib+ZrQiQmeBPzGWX4yiSM8eMDrP2sC8r5pJFMp5DRONwJBAJ4n4XuSFJ9jgwCPy3vvzSv9SYLk6E6yM9uHdUlKgoGYzk6Lh6M9QFuY/J49plFdBDiEnj16yCU3WeXXfTJpzB8CQQDMNMR/rIOTE9xGybS3mlQbt22AUnO6XhupWcckEKW4nPGxATwYBQzCY3i/9FTGN0vA+9ZPC2cwHtNxI2kXf3Vp";  // 商户私钥
    private static final String payUrl = "https://id-openapi.toppay.asia/gateway/prepaidOrder";
    private static final String cashUrl = "https://id-openapi.toppay.asia/gateway/cash";
    private static final String payNotify = "your notify url";
    private static final String cashNotify = "your notify url";

    public static void main(String[] args) throws Exception {
        // 代收
        pay();
    }

    private static void pay() throws Exception {
        Map<String, String> requestParams = new TreeMap<>();
        requestParams.put("merchantCode", MCH_ID);
        requestParams.put("orderType", "0"); // 订单类型（0-法币交易）
        requestParams.put("method", "BCA"); // 支付渠道
        requestParams.put("orderNum", "T1642592278863"); // 商户订单号
        requestParams.put("payMoney", "150");  // 订单金额
        requestParams.put("name", "JackMa");// 客户名称
        requestParams.put("phone", "082122965511"); // 手机号
        requestParams.put("email", "test@email.com");// 客户邮箱
        requestParams.put("notifyUrl", payNotify);// 回调地址
        requestParams.put("dateTime", "20220101235959");// 时间戳 格式 yyyyMMddHHmmss
        requestParams.put("expiryPeriod", "1440"); // 过期时间 单位(分)
        requestParams.put("productDetail", "Test Pay");// 订单详情
        requestParams.put("redirectUrl", "http://123.com");// 返回跳转地址（仅限收银台使用）

        StringBuilder stringBuilder = new StringBuilder();
        for (String key : requestParams.keySet()) {
            stringBuilder.append(requestParams.get(key));  // 拼接参数
        }

        String keyStr = stringBuilder.toString();  // 得到待加密的字符串
        System.out.println("keyStr:" + keyStr);
        String signedStr = TopPayRequestUtil.privateEncrypt(keyStr, TopPayRequestUtil.getPrivateKey(MCH_PRIVATE_KEY));  // 私钥加密
        requestParams.put("sign", signedStr);

        String postJson = new Gson().toJson(requestParams);
        System.out.println("Post Json Params:" + postJson);

        String responseJson = TopPayRequestUtil.doPost(payUrl, postJson);  // 发送 post json请求
        System.out.println("Response Msg:" + responseJson);
    }
}

